package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Utils.OssUtil;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * UploadController
 *
 * @Author 杭州素色网络科技有限公司
 * @Date 2019/7/26
 * 上传文件
 **/
@Controller
@ResponseBody
@RequestMapping("/upload")
public class UploadController {

    /**
     * 多个文件有序上传
     * 增量上传(更新)
     * @param
     * @return
     */
    @RequestMapping(value = "/imagesWithOrder", method = RequestMethod.POST)
    public String uploadImagesWithOrder(HttpServletRequest request){
        RegEntity regInfo = new RegEntity();
        String isImageEmpty = request.getParameter("isImageEmpty");
        String defaultImags = request.getParameter("defaultImags");

        String keys = defaultImags;
        // 图片是否清空
        if (Integer.parseInt(isImageEmpty) != 0) {
            // 转型为MultipartHttpRequest：
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            // 一、 获得文件 ,上传文件
            OssUtil ossUtil = new OssUtil();

            int flag = 1;
            for (int i = 0; i < 5; i++) {
                MultipartFile file = multipartRequest.getFile("file" + i);
                if (file == null)
                    break;
                else {
                    Map<String, Object> result = ossUtil.upload(file);
                    flag = (Integer) result.get("status");
                    if (flag == 0) {
                        break;
                    } else {
                        String key = (String) result.get("key");
                        keys += key + ",";
                    }
                }
            }

            if (!"".equals(keys)) {
                keys = keys.substring(0, keys.length() - 1);
            }

            if (flag == 0) {
                // 上传失败
                regInfo.setInfoCode(-11);
                regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(-11));
                return JSON.toJSONString(regInfo);
            }
        }
        regInfo.setInfoCode(11);
        regInfo.setInfoText(keys);
        return JSON.toJSONString(regInfo);
    }

    /**
     * 多个文件无序上传
     * @param files
     * @return
     */
    @RequestMapping(value = "/imagesWithDisorder", method = RequestMethod.POST)
    public String uploadImagesWithDisorder(@RequestParam("files") MultipartFile[] files) {
        String keys = "";
        //判断file数组不能为空并且长度大于0
        if (files != null && files.length > 0) {
            // 一、 获得文件 ,上传文件
            OssUtil ossUtil = new OssUtil();
            int flag = 1;
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];

                if (file == null)
                    break;
                else {
                    Map<String, Object> result = ossUtil.upload(file);
                    flag = (Integer) result.get("status");
                    if (flag == 0) {
                        break;
                    } else {
                        String key = (String) result.get("key");
                        keys += key + ",";
                    }
                }
            }
        }
        return keys;
    }


}
